package io.lkt.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lkt.common.utils.Constant;
import io.lkt.common.utils.ImageToBase64;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.Query;
import io.lkt.modules.sys.dao.BUserInfoDao;
import io.lkt.modules.sys.dao.SysUserGzhDao;
import io.lkt.modules.sys.entity.*;
import io.lkt.modules.sys.service.BTeamRelationService;
import io.lkt.modules.sys.service.BUserInfoService;
import io.lkt.modules.sys.shiro.WeixinService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpUserBlacklistService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("bUserInfoService")
public class BUserInfoServiceImpl extends ServiceImpl<BUserInfoDao, BUserInfoEntity> implements BUserInfoService {
    @Autowired
    BUserInfoDao bUserInfoDao;

    @Autowired
    SysUserGzhDao sysUserGzhDao;

    @Autowired
    private BTeamRelationService bTeamRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String user_id = (String) params.get("user_id");
        String nick_name = (String) params.get("nick_name");
        String referer = (String) params.get("referer");
        IPage<BUserInfoEntity> page = this.page(
                new Query<BUserInfoEntity>().getPage(params),
                new QueryWrapper<BUserInfoEntity>().eq("concern_status", 1).like(StringUtils.isNotBlank(user_id), "user_id", user_id).like(StringUtils.isNotBlank(nick_name), "nick_name", nick_name).like(StringUtils.isNotBlank(referer), "referer", referer).apply(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER)
                ));
        return new PageUtils(page);
    }

    @Override
    public String vipTime(Integer userId) {
        return bUserInfoDao.vipTime(userId);
    }

    @Override
    public BUserVipValidEntity getVipValid(Integer userId) {
        return bUserInfoDao.getVipValid(userId);
    }

    @Override
    public Integer addVipLog(Map<String, Object> map) {
        return bUserInfoDao.addVipLog(map);
    }

    @Override
    public Integer vipValidTotal(Integer userId) {
        return bUserInfoDao.vipValidTotal(userId);
    }

    @Override
    public Integer addVipValid(Map<String, Object> map) {
        return bUserInfoDao.addVipValid(map);
    }

    @Override
    public Integer setVipValid(Map<String, Object> map) {
        return bUserInfoDao.setVipValid(map);
    }

    @Override
    public Map<String, Object> mainTotal() {
        return bUserInfoDao.mainTotal();
    }

    @Override
    public List<BUserInfoEntity> queryList(Map<String, Object> map) {
        return bUserInfoDao.queryList(map);
    }

    @Override
    public List<BUserVitalEntity> queryUserVital(Map<String, Object> map) {
        return bUserInfoDao.queryUserVital(map);
    }

    @Override
    public List<BUserInfoEntity> queryToDaySubUsers(String gzhCode) {
        return bUserInfoDao.queryToDaySubUsers(gzhCode);
    }

    @Override
    public List<BUserVitalEntity> queryUserVitalByGzh(Map<String, Object> map) {
        return bUserInfoDao.queryUserVitalByGzh(map);
    }

    @Override
    public Integer queryUserVitelTotal(Map<String, Object> map) {
        return bUserInfoDao.queryUserVitelTotal(map);
    }

    @Override
    public String getBrandNameByUserId(Integer userId) {
        return bUserInfoDao.getBrandNameByUserId(userId);
    }

    @Override
    public Integer userTotal(Map<String, Object> map) {
        return bUserInfoDao.userTotal(map);
    }

    @Override
    public Integer refererTotal(Map<String, Object> map) {
        return bUserInfoDao.refererTotal(map);
    }

    @Override
    public Integer fufei(Integer referer) {
        return bUserInfoDao.fufei(referer);
    }

    @Override
    public List<BUserInfoEntity> referer(Map<String, Object> map) {
        return bUserInfoDao.referer(map);
    }

    @Override
    public List<BUserInfoEntity> secondReferer(Map<String, Object> map) {
        return bUserInfoDao.secondReferer(map);
    }

    @Override
    public Integer secondTotal(Map<String, Object> map) {
        return bUserInfoDao.secondTotal(map);
    }

    @Override
    public Integer secondFufei(Map<String, Object> map) {
        return bUserInfoDao.secondFufei(map);
    }

    @Override
    public Integer secondRefererTotal(Map<String, Object> map) {
        return bUserInfoDao.secondRefererTotal(map);
    }

    @Override
    public int deleteVirtualVisitUsers(List<String> virtualUserIds, Integer userId) {
        return bUserInfoDao.deleteVirtualVisitUsers(virtualUserIds, userId);
    }

    @Override
    public List<BrandUserEntity> brandUser(Map<String, Object> map) {
        return bUserInfoDao.brandUser(map);
    }

    @Override
    public List<BrandUserEntity> brandPayUser(Map<String, Object> map) {
        return bUserInfoDao.brandPayUser(map);
    }

    @Override
    public int brandUserTotal(Map<String, Object> map) {
        return bUserInfoDao.brandUserTotal(map);
    }

    @Override
    public int brandPayUserTotal(Map<String, Object> map) {
        return bUserInfoDao.brandPayUserTotal(map);
    }

    @Override
    public BrandUserEntity brandUserHead(Map<String, Object> map) {
        return bUserInfoDao.brandUserHead(map);
    }

    @Override
    public int blockByUser(Integer userId) {
        blockLinkByUser(userId, 1);//在公众号中拉黑
        return bUserInfoDao.blockByUser(userId);
    }

    @Override
    public int cancelBlockByUser(Integer userId) {
        blockLinkByUser(userId, 2);
        return bUserInfoDao.cancelBlockByUser(userId);
    }

    @Override
    public int handle(Integer id) {
        return bUserInfoDao.handle(id);
    }

    public List<BtipOffInfoEntity> queryComplainList(Map<String, Object> map) {
        return this.bUserInfoDao.queryComplainList(map);
    }

    public Integer queryComplainTotal(Map<String, Object> map) {
        return this.bUserInfoDao.queryComplainTotal(map);
    }

    @Override
    public int updateCensus() {
        return this.bUserInfoDao.updateCensus();
    }

    /*在公众号拉黑*
     *
     * @param userId 用户 userId
     * @param blockLinkType 操作类型 1-拉入黑名单 2-拉出黑名单
     * @return
     */
    @Override
    public int blockLinkByUser(Integer userId, Integer blockLinkType) {
        if (userId == null || blockLinkType == null) {
            return 0;
        }

        BUserInfoEntity userInfo = bUserInfoDao.selectById(userId);
        if (userInfo != null && userInfo.getOpenId() != null) {
            try {
                String gzhCode = userInfo.getGzhCode();
                String openId = userInfo.getOpenId();
                SysGzhEntity sysGzh = sysUserGzhDao.getGzhByCode(gzhCode);

                WxMpServiceImpl wxMpService = WeixinService.getWeixinService(sysGzh);
                WxMpUserBlacklistService blackListService = wxMpService.getBlackListService();
                List<String> var1 = new ArrayList();
                var1.add(openId);
                if (blockLinkType == 1) {
                    blackListService.pushToBlacklist(var1);
                }

                if (blockLinkType == 2) {
                    blackListService.pullFromBlacklist(var1);
                }

            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }

        return 1;
    }

    @Override
    public List<UserChildrenEntity> getSubordinate(Integer userId) {

        QueryWrapper<BUserInfoEntity> queryWrapper = new QueryWrapper();

        queryWrapper.select("user_id", "nick_name", "avatar_url")
                .eq("referer", userId)
                .eq("block_state", 0)
                .orderByAsc("user_id");

        List<BUserInfoEntity> temporary = bUserInfoDao.selectList(queryWrapper);

        List<UserChildrenEntity> infoList = new ArrayList<>();

        for (BUserInfoEntity info: temporary) {
            UserChildrenEntity e = new UserChildrenEntity();

            e.setName(info.getNickName());
            e.setValue(info.getUserId());
            e.setSymbol(info.getAvatarUrl());
            e.setSymbolSize(25);

            infoList.add(e);
        }

        return infoList;
    }

    @Override
    public BUserInfoEntity getSelfUser(Integer userId){
        return bUserInfoDao.getSelfUser(userId);
    }

    @Override
    public List<BUserInfoEntity> getSubUser(Integer userId) {
        return bUserInfoDao.getSubUser(userId);
    }

    @Override
    public Integer getSubscribeCountByAppId(String appId) {
        return Optional.ofNullable(this.bUserInfoDao.getSubscribeCountByAppId(appId)).orElse(0);
    }

    @Override
    public Integer getUnSubscribeCountByAppId(String appId) {
        return Optional.ofNullable(this.bUserInfoDao.getUnSubscribeCountByAppId(appId)).orElse(0);
    }

    @Override
    public Integer getSubscribeCountByDate(String gzhCode,String startDate,String endDate){
        return Optional.ofNullable(this.bUserInfoDao.getSubscribeCountByDate(gzhCode,startDate,endDate)).orElse(0);
    }

    @Override
    public Integer getUnSubscribeCountByDate(String gzhCode,String startDate,String endDate){
        return Optional.ofNullable(this.bUserInfoDao.getUnSubscribeCountByDate(gzhCode,startDate,endDate)).orElse(0);
    }

    @Override
    public Integer getSubUserCountByAppId(String gzhCode) {
        return Optional.ofNullable(this.bUserInfoDao.getSubUserCountByAppId(gzhCode)).orElse(0);
    }

    @Override
    public Integer getUnSubUserCountByAppId(String gzhCode) {
        return Optional.ofNullable(this.bUserInfoDao.getUnSubUserCountByAppId(gzhCode)).orElse(0);
    }

    @Override
    public List<BUserPunchClockInfoEntity> getUserClockInfo(Map<String, Object> params) {
        return bUserInfoDao.getUserClockInfo(params);
    }

    @Override
    public Integer getClockInfoTotal(Map<String, Object> params) {
        return bUserInfoDao.getClockInfoTotal(params);
    }

    @Override
    public int getSubUserCount(Integer userId) {
        return bUserInfoDao.getSubUserCount(userId);
    }

    @Override
    public Integer queryTodayComplainTotal(Map<String, Object> map) {
        return bUserInfoDao.queryTodayComplainTotal(map);
    }

    @Override
    public BtipOffInfoEntity getLatestComplain(Map<String, Object> map) {
        return bUserInfoDao.getLatestComplain(map);
    }

    @Override
    public BUserGroupOpenidEntity getMainUserId(Integer userId) {
        return bUserInfoDao.getMainUserId(userId);
    }

    @Override
    public List<BUserInfoEntity> getUserOtherFollowGzh(Map<String, Object> map) {
        return bUserInfoDao.getUserOtherFollowGzh(map);
    }

    @Override
    public Integer getUserOtherFollowGzhTotal(Map<String, Object> map) {
        return bUserInfoDao.getUserOtherFollowGzhTotal(map);
    }

    @Override
    public String  getUnionId(Integer userId) {
        return bUserInfoDao.getUnionId(userId);
    }

    @Override
    public List<BUserInfoEntity> getReferrerInfo(Map<String, Object> map) {
        return bUserInfoDao.getReferrerInfo(map);
    }

    @Override
    public Integer getReferrerInfoTotal(Map<String, Object> map) {
        return bUserInfoDao.getReferrerInfoTotal(map);
    }

    @Override
    public String getUserName(Integer userId) {
        return bUserInfoDao.getUserName(userId);
    }

    @Override
    public List<BUserInfoEntity> getUnsubscribeUser(Map<String, Object> map) {
        return bUserInfoDao.getUnsubscribeUser(map);
    }

    @Override
    public Integer getUnsubscribeUserTotal(Map<String, Object> map) {
        return bUserInfoDao.getUnsubscribeUserTotal(map);
    }

    @Override
    public List<BUserInfoEntity> getNoWorthUser(Map<String, Object> map) {
        return bUserInfoDao.getNoWorthUser(map);
    }

    @Override
    public Integer getNoWorthUserTotal(Map<String, Object> map) {
        return bUserInfoDao.getNoWorthUserTotal(map);
    }

    @Override
    public BUserInfoEntity queryUserInfo(Integer userId) {
        return bUserInfoDao.queryUserInfo(userId);
    }
}
